/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.lua;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class LuaAsyncThread implements Runnable{
    private List<Runnable> tasks = Collections.synchronizedList(new ArrayList<Runnable>(64));
    private static LuaAsyncThread instance = null;
    public void run() {
        while(true)
        {
            synchronized(tasks)
            {
                try {
                    for (Runnable r : tasks) {
                        r.run();
                    }
                    tasks.clear();
                    tasks.wait();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    public void add(Runnable r)
    {
        synchronized(tasks)
        {
            tasks.add(r);
            tasks.notify();
        }
    }
    public static LuaAsyncThread getInstance()
    {
        return instance;
    }
    static {
        instance = new LuaAsyncThread();
        Thread t = new Thread(instance);
        t.setName("LuaEventQueue");
        t.start();
    }
}
